package com.example.spindrift.service;

import com.example.spindrift.dao.SysNotifMapper;
import com.example.spindrift.dao.SysSongListMapper;
import com.example.spindrift.dao.SysUserNotifMapper;
import com.example.spindrift.models.SysNotif;
import com.example.spindrift.models.SysSong;
import com.example.spindrift.models.SysSongList;
import com.example.spindrift.models.SysUserNotif;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SongListService {
    @Autowired
    SysSongListMapper sysSongListMapper;
    @Autowired
    SysNotifMapper sysNotifMapper;
    @Autowired
    SysUserNotifMapper sysUserNotifMapper;

    public List<SysSongList> getAllSongList() {
        return sysSongListMapper.getAllSongList();
    }

    public List<SysSongList> searchSongList(String keyword,String states) {
        return sysSongListMapper.searchSongList(keyword,states);
    }

    public List<SysSongList> getSongListByUser(Integer userId) {
        return sysSongListMapper.getSongListByUser(userId);
    }

    public SysSongList getSongListById(Integer songListId){
        return sysSongListMapper.getSongListById(songListId);
    }
    public List<SysSong> getSongBySongList(Integer songListId) {
        return sysSongListMapper.getSongBySongList(songListId);
    }
    public List<SysSong> searchSongBySongList(Integer songListId,String keyword) {
        return sysSongListMapper.searchSongBySongList(songListId,keyword);
    }
    public List<SysSongList> getSongListByState(String states){
        return sysSongListMapper.getSongListByState(states);
    }

    public Integer insertSongList(SysSongList songList) {
        return sysSongListMapper.insertSongList(songList);
    }

    public Integer updateSongList(SysSongList songList) {
        return sysSongListMapper.updateSongList(songList);
    }
    public Boolean issueSongList(SysSongList songList){
        songList.state = "1";// 设置歌单状态为待审批
        return sysSongListMapper.updateSongList(songList) > 0;
    }

    /**
     * 审批歌单
     * @param songListId
     * @return
     */
    public Boolean approveSongList(Integer songListId,Boolean isPass,Integer approveUserId){
        SysSongList list = sysSongListMapper.getSongListById(songListId);
        list.state = isPass ? "2" : "3";
        Boolean isSuccess = sysSongListMapper.updateSongList(list) > 0;
        if(isSuccess){
            SysNotif notif = new SysNotif();
            notif.title = "审批进度通知";
            notif.notif_type = 1;
            notif.send_user_id = approveUserId;
            notif.read_state = "1";
            if(isPass){
                notif.message = "你分享的歌单已通过审批，赶紧去看看吧！";
            }else{
                notif.message = "很抱歉，你分享的歌单未能通过审批，请按规则填写好表单信息后尝试再次分享。";
            }
            sysNotifMapper.insertNotif(notif);
            SysUserNotif notifToUser = new SysUserNotif();
            notifToUser.notif_id = notif.notif_id;
            notifToUser.user_id = list.user_id;
            sysUserNotifMapper.insertNotifToUser(notif.notif_id,list.user_id + "");
        }
        return isSuccess;
    }

    public Integer deleteSongList(Integer songListId) {
        return sysSongListMapper.deleteSongList(songListId);
    }
}
